﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace VntNet.PowerSchemeSwitcher
{
    public partial class PowerNotificationForm : Form
    {
        PowerSettingNotificationMsgFilter _pwrNotMsgFilter;
        PowerLineHelper _powerLineHelper;
        string _currentLineName;
        PowerSettingNotificationMsgFilter.PowerSavingLevelEnum _currentPowerSavingLevel;

        internal event Action<string,PowerSettingNotificationMsgFilter.PowerSavingLevelEnum> PowerStatusChangedEvent;


        public PowerNotificationForm()
        {
            InitializeComponent();
            _powerLineHelper = new PowerLineHelper();
            _currentLineName = PowerLineHelper.GetCurrentSystemPowerStatus().ToString();
            _powerLineHelper.OnPowerLineStatusChanged += new Action<PowerLineHelper.PowerLineStatus>(powerLineHelper_OnPowerLineStatusChanged);
        }

        // I have to override win proc as IMessageFilter was not filtering power messages
        protected override void WndProc(ref Message m)
        {
            if (_pwrNotMsgFilter != null)
            {
                if (!_pwrNotMsgFilter.PreFilterMessage(ref m))
                    base.WndProc(ref m);
            }
            base.WndProc(ref m);
        }

        void OnPowerStatusChangedEvent()
        {
            if (PowerStatusChangedEvent != null)
                PowerStatusChangedEvent(_currentLineName,_currentPowerSavingLevel);
        }

        #region form
        private void PowerNotificationForm_Shown(object sender, EventArgs e)
        {
            Hide();
        }

        private void PowerNotificationForm_Load(object sender, EventArgs e)
        {
            AttachPowerSettingNotificationMsgFilter();
        }

        private void PowerNotificationForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            DetachPowerSettingNotificationMsgFilter();
        }
        #endregion

        #region PowerSettingNotification
        private void AttachPowerSettingNotificationMsgFilter()
        {
            _pwrNotMsgFilter = new PowerSettingNotificationMsgFilter(this);
            _pwrNotMsgFilter.OnPowerSchemeChanged += new Action<PowerSettingNotificationMsgFilter.PowerSavingLevelEnum>(pwrNotMsgFilter_OnPowerSchemeChanged);
        }

        private void DetachPowerSettingNotificationMsgFilter()
        {
            _pwrNotMsgFilter.Destroy();
        }

        void powerLineHelper_OnPowerLineStatusChanged(PowerLineHelper.PowerLineStatus obj)
        {
            _currentLineName = PowerLineHelper.GetCurrentSystemPowerStatus().ToString();
        }

        void pwrNotMsgFilter_OnPowerSchemeChanged(PowerSettingNotificationMsgFilter.PowerSavingLevelEnum powerSavingLevel)
        {
           _currentPowerSavingLevel=powerSavingLevel;
            OnPowerStatusChangedEvent();
        }
        #endregion
    }
}
